package com.github.permissiondog.cancanpandroid.repository.impl;

import android.util.Log;
import android.widget.Toast;

import com.github.permissiondog.cancanpandroid.MyApplication;
import com.github.permissiondog.cancanpandroid.model.User;
import com.github.permissiondog.cancanpandroid.repository.UserRepository;
import com.github.permissiondog.cancanpandroid.repository.api.UserApiService;
import com.github.permissiondog.cancanpandroid.repository.api.request.LoginRequest;
import com.github.permissiondog.cancanpandroid.repository.api.response.BaseResponse;
import com.github.permissiondog.cancanpandroid.repository.api.response.NavResponse;
import com.github.permissiondog.cancanpandroid.util.CallBack;
import com.github.permissiondog.cancanpandroid.util.EmptyCallBack;
import com.github.permissiondog.cancanpandroid.util.NetUtil;

import java.util.Optional;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserRepositoryImpl implements UserRepository {
    private static UserRepository INSTANCE;
    public static synchronized UserRepository getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new UserRepositoryImpl();
        }
        return INSTANCE;
    }
    private final UserApiService userApiService;
    private UserRepositoryImpl() {
        this.userApiService = NetUtil.RETROFIT.create(UserApiService.class);
    }
    @Override
    public void login(String username, String password, CallBack<Boolean> ok) {
        var loginRequest = new LoginRequest();
        loginRequest.setUsername(username);
        loginRequest.setPassword(password);

        userApiService.login(loginRequest).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                if (!response.isSuccessful() || response.body() == null) {
                    Toast.makeText(MyApplication.getInstance().getApplicationContext(), "网络错误", Toast.LENGTH_LONG).show();
                    return;
                }
                ok.callback(response.body().isOk());
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                Log.w("request", "登录失败", t);
            }
        });
    }

    @Override
    public void currentUser(CallBack<Optional<User>> ok) {
        userApiService.nav().enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<NavResponse> call, Response<NavResponse> response) {
                if (!response.isSuccessful() || response.body() == null) {
                    Toast.makeText(MyApplication.getInstance().getApplicationContext(), "网络错误", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!response.body().isLoggedIn()) {
                    ok.callback(Optional.empty());
                    return;
                }
                ok.callback(Optional.of(response.body().getUser()));
            }

            @Override
            public void onFailure(Call<NavResponse> call, Throwable t) {
                Log.w("request", "获取当前用户失败", t);
            }
        });
    }

    @Override
    public void logout(EmptyCallBack res) {
        userApiService.logout().enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                res.callback();
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                Log.w("request", "登出失败", t);
            }
        });
    }
}
